﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Threading;
using System.Windows.Threading;

using Factonomy.AzureUtility.Core;
using Factonomy.AzureUtility.Core.Storage;
using Factonomy.AzureUtility.Core.StorageHelpers;

namespace Factonomy.AzureUtility.Controls
{
    class ContainerContextMenu : BaseContextMenu
    {
        //public event TreeViewItemHelperDelegate ContainerDeleted;
        //public event NoParameterDelegate BlobAdded;

        //protected virtual void OnBlobAdded()
        //{
        //    if (BlobAdded != null)
        //        BlobAdded();
        //}

        //protected virtual void OnContainerDeleted(TreeViewItemHelper helper)
        //{
        //    if (ContainerDeleted != null)
        //        ContainerDeleted(helper);
        //}

        public ContainerContextMenu()
        {
            MenuItem addBlob = new MenuItem();
            addBlob.Header = "Add Blob(s)..";
            addBlob.Click += new System.Windows.RoutedEventHandler(AddBlob);
            this.Items.Add(addBlob);

            MenuItem item = new MenuItem();
            item.Header = "Delete Container";
            item.Click += new System.Windows.RoutedEventHandler(DeleteContainer);
            this.Items.Add(item);

            MenuItem deleteAllBlobs = new MenuItem();
            deleteAllBlobs.Header = "Delete All Blobs";
            deleteAllBlobs.Click += new System.Windows.RoutedEventHandler(DeleteAllBlobs);
            this.Items.Add(deleteAllBlobs);
        }

        void DeleteAllBlobs(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                TreeViewItem item = (TreeViewItem)((Popup)((ContextMenu)(((MenuItem)sender).Parent)).Parent).PlacementTarget;
                if (item != null)
                {
                    if (item.Tag != null)
                    {
                        if (MessageBox.Show("Really delete?", "Confirm delete", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                        {
                            this._helper = item.Tag as TreeViewItemHelper;
                            this.DeleteAllBlobs(true);
                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        void DeleteContainer(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                TreeViewItem item = (TreeViewItem)((Popup)((ContextMenu)(((MenuItem)sender).Parent)).Parent).PlacementTarget;
                if (item != null)
                {
                    if (item.Tag != null)
                    {
                        if (MessageBox.Show("Really delete?", "Confirm delete", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                        {
                            this._helper = item.Tag as TreeViewItemHelper;
                            ConfigAccount account = Config.Instance.GetAccount(this._helper.AccountName);
                            BlobHelper.Instance.DeleteContainer(account, this._helper.Container);
                            //this.OnContainerDeleted(this._helper);
                            this._helper = null;
                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        void AddBlob(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                TreeViewItem item = (TreeViewItem)((Popup)((ContextMenu)(((MenuItem)sender).Parent)).Parent).PlacementTarget;
                if (item != null)
                {
                    if (item.Tag != null)
                    {
                        this._helper = item.Tag as TreeViewItemHelper;
                        Dialogs.UploadBlob addBlobDialog = new Dialogs.UploadBlob();
                        addBlobDialog.AddBlob += new Dialogs.UploadBlob.AddBlobDelegate(OnAddNewBlobAdded);
                        addBlobDialog.Owner = Application.Current.Windows[0];
                        addBlobDialog.ShowDialog();
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        private AddBlobDetails _details = null;

        void OnAddNewBlobAdded(AddBlobDetails details)
        {
            this._details = details;

            if (this._details.Action == AddBlobDetails.AddActionType.File)
            {
                ConfigAccount account = Config.Instance.GetAccount(this._helper.AccountName);
                BlobHelper.Instance.AddFile(account, this._helper.Container, this._details.FileName, this._details.BlobName);
                this._details = null;
                this._helper = null;
            }
            else
            {
                this.UploadDirectory(true);
            }


        }

        public void DeleteAllBlobs(object startThread)
        {
            if (bool.Parse(startThread.ToString()))
            {
                Thread thread = new Thread(new ParameterizedThreadStart(this.DeleteAllBlobs));
                thread.IsBackground = true;
                thread.Start(false);
            }
            else
            {
                ConfigAccount account = Config.Instance.GetAccount(this._helper.AccountName);
                BlobHelper.Instance.DeleteAllBlobs(account, this._helper.Container);
                this._helper = null;
            }
        }

        public void UploadDirectory(object startThread)
        {
            if (bool.Parse(startThread.ToString()))
            {
                Thread thread = new Thread(new ParameterizedThreadStart(this.UploadDirectory));
                thread.IsBackground = true;
                thread.Start(false);
            }
            else
            {
                ConfigAccount account = Config.Instance.GetAccount(this._helper.AccountName);
                BlobHelper.Instance.AddDirectory(account, this._helper.Container, this._details.FileName, this._details.BlobName, true);
                this._details = null;
                this._helper = null;
            }
        }
    }
}
